package app

import "time"

type DateTime time.Time

const (
    DateTimeFormart = "2006-01-02 15:04:05"
    DateFormart     = "2006-01-02"
)

func (t *DateTime) UnmarshalJSON(data []byte) (err error) {
    now, err := time.ParseInLocation(DateTimeFormart, string(data), time.Local)
    *t = DateTime(now)
    return
}

func (t DateTime) MarshalJSON() ([]byte, error) {
    b := make([]byte, 0, len(DateTimeFormart)+2)
    b = append(b, '"')
    b = time.Time(t).AppendFormat(b, DateTimeFormart)
    b = append(b, '"')
    return b, nil
}

func (t DateTime) String() string {
    return time.Time(t).Format(DateTimeFormart)
}

type Date time.Time

func (t *Date) UnmarshalJSON(data []byte) (err error) {
    now, err := time.ParseInLocation(DateFormart, string(data), time.Local)
    *t = Date(now)
    return
}

func (t Date) MarshalJSON() ([]byte, error) {
    b := make([]byte, 0, len(DateFormart)+2)
    b = append(b, '"')
    b = time.Time(t).AppendFormat(b, DateFormart)
    b = append(b, '"')
    return b, nil
}

func (t Date) String() string {
    return time.Time(t).Format(DateFormart)
}
